// pages/orderlist/express/page.js
var app = getApp();
var weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
var shipping_code, invoice_no;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blankshow: true,
    blanktips: '暂时没有物流信息',
    expressLoaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('res:');
    shipping_code = options.shippingcode;
    invoice_no = options.invoiceno;
  },
  loadExpressDetail: function (shipping_code, invoice_no) {
    app.showLoading();
    var url = '/index.php?m=Home&c=Api&a=queryExpress';
    var params = {
      shipping_code: shipping_code,
      invoice_no: invoice_no
    }
    app.request(url,params).then(this.expressDetailCallBack.bind(this));
  },
  expressDetailCallBack: function (res) {
    res = JSON.parse(res);
    // console.log(res);
    app.hideLoading();
    var blankshow = true;
    if (res.status == '200'||res.status == '0') {
      if (res.result.list.length <= 0) {
        blankshow = true;
      }
      else {
        blankshow = false;
      }
    }
    this.setData({
      blankshow: blankshow,
      expressLoaded: true
    });
    if (blankshow == true) {
      return;
    }
    var scheduleData = res.result.list;
    var expressinfo = [];
    for(var i=0;i<scheduleData.length;i++){
      var datestr = scheduleData[i].time;
      var tmparr = datestr.split(' ');
      var dateDay = tmparr[0];
      var dateTime = tmparr[1];

      var flowinfo = {
        time: dateTime,
        content: scheduleData[i].status
      };

      var date = new Date(datestr.replace(/-/g, "/"));
      var weekday = weekdays[date.getDay()];
      var info;
      var needPush;
      if (expressinfo.length > 0){
        if (expressinfo[expressinfo.length - 1].date != dateDay){
          info={};
          needPush = true;
        }
        else{
          info = expressinfo[expressinfo.length - 1];
          needPush = false;
        }
      }
      else{
        needPush = true;
        info = {};
      }
      if(info.date){
        info.flowlist.push(flowinfo);
      }
      else{
        info.date = dateDay;
        info.weekday = weekday;
        info.flowlist = [];
        info.flowlist.push(flowinfo);
      }
      if(needPush){
        expressinfo.push(info);
      }
    }
    for(var i=0;i<expressinfo.length;i++){
      expressinfo[i].flowlist.reverse();
    }
    expressinfo.reverse();
    if (res.state == '3'){
      var tmp = expressinfo[expressinfo.length-1].flowlist;
      tmp[tmp.length - 1].isfinish = '1';
    }
    this.setData({
      expressinfo: expressinfo
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadExpressDetail(shipping_code, invoice_no);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  }
})